$(document).ready(function(){



function build_table(search_string){
//	var current_search_term = 'Wootten, C';
	var table_name = search_string.replace(/ /g,"_");
	var table_name = table_name.replace(/,/g,"_");
	var table_name = table_name.replace(/'/g,"_");

		console.log("search_string = " + search_string);
		console.log("table_name = " + table_name);
	$('body').append(
		'<p></p><div class="attending_name_header" id="' + table_name +'_header">' + search_string + '</div> ' +
		'<table id="' + table_name + '">' +
		'<colgroup>' +
			'<col class="column_one">' +
			'<col class="table_columns column_two">' +
			'<col class="table_columns column_three">' +
			'<col class="table_columns column_four">' +
			'<col class="table_columns column_five">' +
		'</colgroup>' +
		'<thead> ' +
			'<tr>' +
			'<th>Time</th>' +
			'<th>Room</th>' +
			'<th>Procedure</th>' +
			'<th>Patient</th>' +
			'<th>Comments</th>' +
			'</tr>' +
		'</thead>' +
		'<tbody></tbody>' 
	);

	jQuery('td:contains("Surg:' + search_string + '")').filter(function(){
		var number_of_decendants = $(this).find('*').length;

		if (number_of_decendants == 1) {
			var selected_table = $(this).parent().parent().text();
			var attending = $(this).text();
			var location = $(this).parent().prev().children(":first").text(); 
			var time = $(this).parent().prev().children(":nth-child(2)").text();
			var service = $(this).next().text();
			var procedure = $(this).parent().next().children().children(":first").text();
			var patient_name = $(this).parent().prev().children(":nth-child(4)").text();
			var patient_MR = $(this).parent().prev().children(":nth-child(6)").text();
			patient_MR = patient_MR.replace("-","");
			var patient_DOB = $(this).parent().prev().children(":nth-child(7)").text();
			var patient_age = patient_DOB.substring(patient_DOB.indexOf("(")+1,patient_DOB.length-1);
			patient_DOB = patient_DOB.replace("DOB:", "");
			patient_DOB = patient_DOB.substring(1,patient_DOB.indexOf("("));
			var comments = $(this).parent().next().next().next().children(":first").text();
			comments = comments.replace("Case Comments:", "");
			console.log("ATTENDING = " + $(this).text()); 
			console.log("LOCATION = " + $(this).parent().prev().children(":first").text()); 
			console.log("TIME = " + $(this).parent().prev().children(":nth-child(2)").text()); 
			console.log("SERVICE = " + $(this).next().text()); 
			console.log("PROCEDURE = " + $(this).parent().next().children().children(":first").text()); 
			console.log("PATIENT = " + $(this).parent().prev().children(":nth-child(4)").text()); 
			console.log("MR = " + $(this).parent().prev().children(":nth-child(6)").text()); 
			console.log("DOB = " + $(this).parent().prev().children(":nth-child(7)").text()); 
			console.log("COMMENTS = " + $(this).parent().next().next().next().children(":first").text()); 
				
			$('#' + table_name + '').find('tbody').append(
				'<tr>' + 
					'<td><span class="time">' + time + '</span></td>' + 
					'<td><span class="location">' + location + '</span></td>' + 
					'<td><span class="procedure">' + procedure + '</span></td>' + 
					'<td><span class="patient_name">' + patient_name + '</span><br><span class="patient_MR">' + patient_MR + '</span><br><span class="patient_DOB">' + patient_DOB + '</span><br><span class="patient_age">' + patient_age + '</span></td>' + 
					'<td><span class="comments">' + comments + '</span></td>' + 
				'</tr>'
			);	
		};
	});
};
build_table('Chinnadurai, S');
build_table('Gelbard, A');
build_table('Goudy, S');
build_table('Haynes, D');
build_table('Johnston, B');
build_table('Mannion, K');
build_table('Rivas Campo, A');
build_table('Stephan, S');
build_table('Wootten, C');
   
$('#Wootten__C').addClass('hidden');
$('#Wootten__C' + '_header').addClass('hidden');



});